//
//  XS_BaseBottomAlertVC.swift
//  Pods
//
//  Created by 韩云智 on 2020/7/15.
//

import UIKit
import RxSwift
import RxCocoa

open class XS_BaseBottomAlertVC: XS_BaseVC {
    open var customView: UIView!
    open var shouldClose: Bool = false
    open lazy var actionSubject = PublishSubject<(XS_ActionType, (((() -> Void)?)->())?)>()
    open lazy var titleLab = UILabel.xsLabel(size: 17, alignment: .center)
    
    open override func upConfig() {
        modalTransitionStyle = .crossDissolve
        modalPresentationStyle = .overFullScreen
    }
    open override func upView() {
        view.alpha = 0
        view.backgroundColor = UIColor(white: 0, alpha: 0.7)
    }
    
    open override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        guard let customView = customView else { return }
        if customView.superview == nil {
            view.addSubview(customView)
        }
        if view.alpha == 0 {
            open()
        }
        view.alpha = 1
    }
    /// 点击空白处收起页面
    open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if shouldClose {
            var set = true;
            for touch in touches {
                if touch.view == customView {
                    set = false
                }
            }
            if set {
                actionSubject.onNext((.cancel, { [weak self] completion in self?.close(completion: completion) }))
            }
        }
    }
    
    /// 打开收起动画
    open func open() {
        UIView.animate(withDuration: 0.25) {
            var frame = self.customView.frame
            switch self.orientations {
            case .landscape, .landscapeLeft, .landscapeRight:
                frame.origin.y = .screenWidth - frame.size.height
            default:
                frame.origin.y = .screenHeight - frame.size.height
            }
            self.customView.frame = frame
        }
    }
    open func close(completion: (() -> Void)? = nil) {
        UIView.animate(withDuration: 0.25, animations: {
            var frame = self.customView.frame
            switch self.orientations {
            case .landscape, .landscapeLeft, .landscapeRight:
                frame.origin.y = .screenWidth
            default:
                frame.origin.y = .screenHeight
            }
            self.customView.frame = frame
            self.view.alpha = 0
        }) { _ in
            self.dismiss(animated: false, completion: completion)
        }
    }
}

public extension XS_BaseBottomAlertVC {
    open func loadTopView() {
        let cancel = loadButton("取消", color: #colorLiteral(red: 0.4, green: 0.4, blue: 0.4, alpha: 1), type: .cancel)
        let sure = loadButton("确定", color: #colorLiteral(red: 0.9176470588, green: 0.3294117647, blue: 0.01568627451, alpha: 1), type: .sure)
        let line = UIView()
        line.backgroundColor = #colorLiteral(red: 0.9294117647, green: 0.9294117647, blue: 0.9294117647, alpha: 1)
        customView.addSubview(titleLab)
        customView.addSubview(cancel)
        customView.addSubview(sure)
        customView.addSubview(line)
        cancel.snp.makeConstraints { maker in
            maker.width.equalTo(92)
            maker.height.equalTo(40)
            maker.left.top.equalToSuperview()
        }
        sure.snp.makeConstraints { maker in
            maker.width.equalTo(92)
            maker.height.equalTo(40)
            maker.right.top.equalToSuperview()
        }
        titleLab.snp.makeConstraints { maker in
            maker.centerX.equalToSuperview()
            maker.centerY.equalTo(cancel.snp.centerY)
        }
        line.snp.makeConstraints { maker in
            maker.left.right.equalToSuperview()
            maker.top.equalToSuperview().offset(40)
            maker.height.equalTo(1)
        }
    }
    
    open func loadButton(_ text: String, color: UIColor, type: XS_ActionType) -> UIButton {
        let button = UIButton()
        button.titleLabel?.font = .xsFont(16)
        button.setTitleColor(color, for: .normal)
        button.setTitle(text, for: .normal)
        button.rx.tap.asDriver()
            .throttle(.seconds(2), latest: false)
            .map({[weak self] _ in
                return (type, { [weak self] completion in self?.close(completion: completion) })
            }).drive(actionSubject)
            .disposed(by: disposeBag)
        return button
    }
}
